// created by noellynch
// May 15, 2009

package com.sceneworker.editors.beanshelleditor;

import java.util.ArrayList;
import java.util.concurrent.Callable;

import javax.swing.JDialog;

import bsh.EvalError;
import bsh.Interpreter;
import bsh.NameSpace;

import com.jme.scene.Spatial;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.SimpleAlert;
import com.sceneworker.editors.beanshelleditor.scriptsnippets.OneOffSnippetManager;

public class BeanShellOneOffScriptPanel extends BeanShellEditorPanel {
	
	static	private		ArrayList<String>		mcla_jmeKeyWords;
	
	public	BeanShellOneOffScriptPanel(JDialog cl_par, Spatial cl_spatial) {
		super("// the execute method will be called in the update method of the app\n// and the object parameter is the spatial that the script will be executed on"
				 + "\n\nexecute(object) {\n}", cl_par, cl_spatial, OneOffSnippetManager.inst());
		
		if(mcla_jmeKeyWords == null) {
			initialiseJMEKeyWordsList();
		}
	}	
	
	@Override
	protected	void	executeScript() {
		try {
			mcl_err.setText("");
			final	Interpreter		mcl_interpreter = new Interpreter();
			Object			lcl_temp = mcl_interpreter.eval(mcl_text.getText());
			final	NameSpace		lcl_nm = mcl_interpreter.getNameSpace();
			
			GameTaskQueueManager.getManager().update(new Callable<Void>() {
				public Void call() throws Exception {
					try {
						lcl_nm.invokeMethod("execute", new Object[] { mcl_spatial }, mcl_interpreter);
						SimpleAlert.doAlert("Script Executed.", null);
						//mcl_par.setVisible(false);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						mcl_err.setText(e.getMessage());
					}
					
					return null;
				}
			});
			
			
		} catch (EvalError e) {
			// TODO Auto-generated catch block
			mcl_err.setText(e.getMessage());
		}
	}
	
	private	void	initialiseJMEKeyWordsList() {
		
	}
}
